/**
 * Licensed to Apereo under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright ownership. Apereo
 * licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the License at the
 * following location:
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apereo.portal.rendering.predicates;

import java.util.function.Predicate;
import javax.servlet.http.HttpServletRequest;
import org.apereo.portal.portlet.om.IPortletDefinition;
import org.apereo.portal.portlet.om.IPortletDescriptorKey;
import org.apereo.portal.rendering.RequestRenderingPipelineUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Predicate indicating whether the requested portlet has a web app name that contains a configured
 * String.
 *
 * <p>Useful for detecting e.g. maximized requests for a portlet that ought to be redirected to
 * uPortal-home for alternative presentation.
 */
public class WebAppNameContainsStringPredicate implements Predicate<HttpServletRequest> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    private RequestRenderingPipelineUtils utils;

    private String webAppNameToMatch;

    public void setWebAppNameToMatch(String webAppNameParam) {
        this.webAppNameToMatch = webAppNameParam;
    }

    public String getWebAppNameToMatch() {
        return this.webAppNameToMatch;
    }

    @Autowired
    public void setUtils(RequestRenderingPipelineUtils u) {
        this.utils = u;
    }

    @Override
    public boolean test(final HttpServletRequest request) {
        try {
            final IPortletDefinition portletDefinition =
                    utils.getPortletDefinitionFromServletRequest(request);
            IPortletDescriptorKey portletDescriptorKey =
                    portletDefinition.getPortletDescriptorKey();
            String portletWebAppName = portletDescriptorKey.getWebAppName();

            boolean doesMatch = portletWebAppName.contains(this.webAppNameToMatch);

            if (logger.isDebugEnabled()) {
                if (doesMatch) {
                    logger.debug(
                            "Predicate is true: observed web app name [{}]"
                                    + " does match configured name [{}].",
                            portletWebAppName,
                            this.webAppNameToMatch);
                } else {
                    logger.debug(
                            "Predicate is false: observed web app name [{}]"
                                    + " does not match configured name [{}].",
                            portletWebAppName,
                            this.webAppNameToMatch);
                }
            }

            return doesMatch;

        } catch (Exception e) {
            logger.error(
                    "Failed to process web app name contains string check for redirect during pipeline. Failing gracefully by returning false.",
                    e);
        }
        return false;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}
